package org.apache.commons.graph.model

import org.apache.commons.graph.DirectedGraph
import org.apache.commons.graph.VertexPair
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * An adapted class that mirrors [DirectedGraph] instances,
 * inverting vertices relationships.
 *
 * It is useful for algorithms such as Kosaraju-Sharir for strongly connected components search.
 *
 * @param <V> the Graph vertices type
 * @param <E> the Graph edges type
</E></V> */
class RevertedGraph<V, E>( private val directedGraph: DirectedGraph<V, E>) : DirectedGraph<V, E> {
    /**
     * The DirectedGraph has to be adapted.
     */


    override fun containsEdge(e: E): Boolean {
        return directedGraph.containsEdge(e)
    }

    override fun containsVertex(v: V): Boolean {
        return directedGraph.containsVertex(v)
    }

    override fun getConnectedVertices(v: V): Iterable<V> {
        return directedGraph.getConnectedVertices(v)
    }


    override fun getDegree(v: V): Int {
        return directedGraph.getDegree(v)
    }


    override fun getEdge(source: V, target: V): E? {
        return directedGraph.getEdge(target, source)
    }

    override val edges: Iterable<E>
        get() = directedGraph.edges

    /**
     * {@inheritDoc}
     */
    override fun getInbound(v: V): Iterable<V> {
        return directedGraph.getOutbound(v)
    }

    /**
     * {@inheritDoc}
     */
    override fun getInDegree(v: V): Int {
        return directedGraph.getOutDegree(v)
    }

    override val order: Int
        get() = directedGraph.order

    /**
     * {@inheritDoc}
     */
    override fun getOutbound(v: V): Iterable<V> {
        return directedGraph.getInbound(v)
    }

    /**
     * {@inheritDoc}
     */
    override fun getOutDegree(v: V): Int {
        return directedGraph.getOutDegree(v)
    }

    override val size: Int
        get() = directedGraph.size

    override val vertices: Iterable<V>
        get() = directedGraph.vertices

    /**
     * {@inheritDoc}
     */
    override fun getVertices(e: E): VertexPair<V> {
        val directedVertexPair = directedGraph.getVertices(e)
        return VertexPair(directedVertexPair!!.tail, directedVertexPair.head)
    }

}

fun <V, E> DirectedGraph<V, E>.reverted()  = RevertedGraph(this)